package com.qyl.honghengshop.controller.customer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qyl.honghengshop.common.PageConstant;
import com.qyl.honghengshop.dto.Address;
import com.qyl.honghengshop.dto.Order;
import com.qyl.honghengshop.dto.OrderItem;
import com.qyl.honghengshop.dto.User;
import com.qyl.honghengshop.service.OrderItemService;
import com.qyl.honghengshop.service.OrderService;
import com.qyl.honghengshop.service.impl.OrderItemServiceImpl;
import com.qyl.honghengshop.service.impl.OrderServiceImpl;
import com.qyl.honghengshop.utils.CodeGenerate;


@Controller
@RequestMapping("/customer/order")
public class PreOrderController {

	private OrderService orderService = new OrderServiceImpl();
	private OrderItemService orderItemService = new OrderItemServiceImpl();
	
	@RequestMapping("/toOrderSubmit")
	public String toOrderSubmit(Integer[] ids,Double total,HttpSession session,ModelMap map){
		
		//从session中获得购物车
		Map<Integer, Integer> buyCar = (Map<Integer, Integer>) session.getAttribute(PageConstant.BUYCARD);
		
		
		//计算商品数量
		int goodsCount = 0;
		for (Integer id : ids) {
			
			Integer num = buyCar.get(id);
			goodsCount +=num;
		}
		//返回数据
		map.put("goodsCount", goodsCount);
		map.put("total", total);
		String setIds = "";
		for (Integer id : ids) {
			setIds = setIds+ id + "-" ; 
		}
		
		map.put("ids",setIds);
		
		return "confirm_order";
		
	}
	
	
	@RequestMapping("/addorder")
	public String addorder(Integer[] ids,Integer addressId,Double total,HttpSession session,ModelMap map){
		
		//从session中获得购物车
		Map<Integer, Integer> buyCar = (Map<Integer, Integer>) session.getAttribute(PageConstant.BUYCARD);
		
		//从session中获得User对象
		User user = (User) session.getAttribute(PageConstant.SESSION_USER);
		
		//创建并封装好订单对象
		Order order = new Order();
		
		Address address = new Address();
		address.setId(addressId);
		order.setOrder_code(CodeGenerate.generateOrderCode());
		order.setCreate_date(new Date());
		order.setTotal_amount(total);
		order.setUser(user);
		order.setAddress(address);
		
		//调用service保存订单与订单中间表
		orderService.saveOrder(order, ids, buyCar);

		map.put("orderCode", order.getOrder_code());
		
		map.put("total", total);
		
		return "pay";
	
	}
	
	@RequestMapping("/toUserInfo")
	public String toUserInfo(HttpSession session,ModelMap map){
		
		//从session中获得User对象
		User user = (User) session.getAttribute(PageConstant.SESSION_USER);
		
		map.put("user", user);
		
		return "user";
	}
	
	@RequestMapping("/toMyOrder")
	public String toMyOrder(ModelMap map,HttpSession session,String trading_status){
		
		User user = (User) session.getAttribute(PageConstant.SESSION_USER);
		
		List<OrderItem> orderItems = orderItemService.getOrderItemList(user);
		
		List<OrderItem> list = null;
		
		if("1".equals(trading_status)){
			
			list = new ArrayList<OrderItem>();
			
			for (OrderItem orderItem : orderItems) {
				
				if(orderItem.getOrder().getTrading_status() == 1){					
					list.add(orderItem);					
				}
			}

		}
		if(list != null){
			map.put("orderItems", list);
		}else{
			map.put("orderItems", orderItems);			
		}
		
		return "order";
	}
	
}
